package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.*;
import cn.edu.scau.dropship.ServiceImpl.FinanceServiceImpl;
import cn.edu.scau.dropship.service.FinanceService;
import cn.edu.scau.dropship.service.TraceService;
import cn.edu.scau.dropship.service.UserService;
import cn.edu.scau.dropship.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/finance")
public class FinaceController {
    @Autowired
    private FinanceService financeService;
    @Autowired
    private TraceService traceService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private UserService userService;

    // 查询finance、trace、wallet三表，组装数据完整交易记录
    @PostMapping(value = "/getFinList")
    @ResponseBody
    public List<Object> getFinList(HttpServletResponse response){
        //组装数据
        List<Object> resultlist=new ArrayList<>();
        List<Finance_check> flist=financeService.getlist();
        List<TraceKey> tlist=new ArrayList<>();
        for(int i=0;i<flist.size();i++){
            TraceKey tk=traceService.selectByfid(flist.get(i).getId());
            Wallets wallets=walletService.selectByPrimaryKey(tk.getWalletsId());
            User user=userService.selectByPrimaryKey(wallets.getUserId());
            resultlist.add(flist.get(i));
            resultlist.add(user);
        }
        return resultlist;
    }

    @PostMapping(value = "/updateState")
    @ResponseBody
    public String updateState(HttpServletRequest request) {
        //获取前台传值
        Integer fid=Integer.valueOf(request.getParameter("id"));
        String uName=request.getParameter("uName");
        String type=request.getParameter("type");
        Double money=Double.valueOf(request.getParameter("money"));
        String state=request.getParameter("state");

        //根据fid获取一条完整finance记录
        Finance_check finance_check=financeService.selectByPrimaryKey(fid);

        //根据state判断是否通过，是否需要改变wallet
        if(state.equals("不通过")){
            finance_check.setState("已审核(不通过)");
            financeService.updateByPrimaryKey(finance_check);
            return "审核成功(不通过)!";
        }

        //根据uName找对应的wid
        User user=userService.selectByName(uName);
        Integer uid=user.getId();
        Wallets wallets=walletService.selectByUid(uid);

        //获取账户余额
        Double balance=wallets.getMoney();

        //根据type判断是充值还是取现
        if(type.equals("充值")){
            wallets.setMoney((balance+money));
            int result=walletService.updateByPrimaryKey(wallets);
            if(result>0){
                finance_check.setState("已审核(通过)");
            }
            else {
                finance_check.setState("数据库请求失败!");
            }
            financeService.updateByPrimaryKey(finance_check);
        }else if(type.equals("提现")&&balance>=money){
            wallets.setMoney(balance-money);
            int result=walletService.updateByPrimaryKey(wallets);
            if(result>0){
                finance_check.setState("已审核(通过)");
            }
            else {
                finance_check.setState("数据库请求失败!");
            }
            financeService.updateByPrimaryKey(finance_check);
        }
        return "审核成功(通过)!";
    }

    /*@GetMapping(value = "/selectById{id}")
    public Finance_check selectById(@RequestParam("id") Integer id) {
        Finance_check finance_check = financeService.selectByPrimaryKey(id);
        return finance_check;
    }*/

    /*@GetMapping(value = "/selectAll")
    public List<Finance_check> selectAll() {
        Finance_checkExample finance_checkExample = new Finance_checkExample();
        Finance_checkExample.Criteria criteria = finance_checkExample.createCriteria();
        criteria.andIdGreaterThan(0);
        List<Finance_check> list = financeService.selectByExample(finance_checkExample);
        return list;
    }*/

    //根据账户名查看流水记录,多表组装
    @PostMapping(value = "/selectByName")
    @ResponseBody
    public List<Object> selectByName(HttpServletRequest request) {
        //组装数据
        List<Object> result=new ArrayList<>();

        String userName=request.getParameter("name");
        // 根据用户名查user表，得到uid
        User user=userService.selectByName(userName);
        Integer uid=user.getId();

        // 根据uid查wallet表，得到wid
        Wallets wallets=walletService.selectByUid(uid);
        Integer wid=wallets.getId();

        // 根据wid查finance表，组装数据
        List<Finance_check> list=financeService.selectByWid(wid);
        for(int i=0;i<list.size();i++){
            result.add(list.get(i));
            result.add(user);
        }

        return result;
    }
}
